package com.kma.summer2012.client.view.closeRequest;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.*;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.*;
import com.kma.summer2012.client.presenters.closerequest.CloseRequestPresenter;
import com.kma.summer2012.client.styles.style2.Recources;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: Naskrina
 * Date: 16.02.13
 * Time: 14:14
 * To change this template use File | Settings | File Templates.
 */
public class CloseRequestView extends Composite implements CloseRequestPresenter.Display {
    interface CloseRequestViewUiBinder extends UiBinder<HTMLPanel, CloseRequestView> {
    }

    private static CloseRequestViewUiBinder ourUiBinder = GWT.create(CloseRequestViewUiBinder.class);


    @UiField
    protected Label pageTitle;

    //Table
    @UiField
    protected TextBox actualTime;
    @UiField
    protected TextArea comment;
    @UiField
    protected ListBox toolsList;
    @UiField
    VerticalPanel toolItems;

    final FlexTable flexTable = new FlexTable();

    public Map<Integer, String> getToolList() {
        return toolList;
    }

    public Map<Integer, Integer> getAmountsValues() {
        for (int key : amounts.keySet()) {
            amountsValues.put(key, Integer.parseInt(amounts.get(key).getValue()));
        }
        return amountsValues;
    }

    Map<Integer, String> toolList = new HashMap<Integer, String>();
    Map<Integer, TextBox> amounts = new HashMap<Integer, TextBox>();
    Map<Integer, Integer> amountsValues = new HashMap<Integer, Integer>();
    Map<Integer, Button> buttonsForToolItem = new HashMap<Integer, Button>();
    //buttons
    @UiField
    protected Button closeButton;
    @UiField
    protected Button cancelButton;

    //getters, setters

    public String getPageTitle() {
        return this.pageTitle.getTitle();
    }

    public void setPageTitle(String title) {
        this.pageTitle.setText(title);
    }

    @Override
    public String getActualTime() {
        return actualTime.getValue();
    }

    @Override
    public void setActualTime(String actualTime) {
        this.actualTime.setValue(actualTime);
    }

    @Override
    public String getComment() {
        return comment.getValue();
    }

    @Override
    public void setComment(String comment) {
        this.comment.setText(comment);
    }

    @Override
    public List<String> getToolsList() {
        List<String> list = new ArrayList<String>();
        for (int i = 0; i < toolsList.getItemCount(); ++i) {
            String item = toolsList.getValue(i);
        }
        return list;
    }

    @Override
    public void setToolsList(List<String> list) {
        this.toolsList.clear();
        for (int i = 0; i < list.size(); ++i) {
            this.toolsList.addItem(list.get(i));
        }
    }

    public HasClickHandlers getCloseButton() {
        return closeButton;
    }

    @Override
    public HasClickHandlers getCancelButton() {
        return cancelButton;
    }


    public Widget asWidget() {
        return this;
    }

    public Widget preWidget() {
        return this;
    }

    public CloseRequestView() {
        HTMLPanel rootElement = ourUiBinder.createAndBindUi(this);
        initWidget(rootElement);
        closeButton.addStyleName(Recources.INSTANCE.styles().Caption());
        cancelButton.addStyleName(Recources.INSTANCE.styles().Caption());
        pageTitle.addStyleName(Recources.INSTANCE.styles().titleCatalog());
        actualTime.addStyleName(Recources.INSTANCE.styles().input());
        comment.addStyleName(Recources.INSTANCE.styles().textarea());


        FlexTable.FlexCellFormatter cellFormatter = flexTable.getFlexCellFormatter();
        cellFormatter.setVerticalAlignment(0, 1, HasVerticalAlignment.ALIGN_TOP);
        flexTable.ensureDebugId("cwFlexTable");
        //WorkField.add(flexTable);
        toolItems.add(flexTable);
        this.toolsList.setVisibleItemCount(5);
        //this.selectedTools.setVisibleItemCount(5);
        this.toolsList.addDoubleClickHandler(new DoubleClickHandler() {
            @Override
            public void onDoubleClick(DoubleClickEvent doubleClickEvent) {
                int selected = toolsList.getSelectedIndex();
                toolList.put(toolList.size(), toolsList.getItemText(selected));
                amountsValues.put((toolList.size() - 1), 1);
                toolsList.removeItem(selected);
                CloseRequestView.this.updateWorkField();
                /*for (UICrumbsWorkItem workItem : showCatalogPresenter.getItemList()) {

                        workItem.setAmount(1);
                        RequestPresenter.this.display.getWorkList().put(workItem.getId(), workItem);
                        currentSum += workItem.getAmount() * workItem.getRuntime();
                    }
                }
                RequestPresenter.this.display.setTotalSumOfWork(currentSum);
                RequestPresenter.this.display.updateWorkField();    */
            }
        });

        /*this.selectedTools.addDoubleClickHandler(new DoubleClickHandler() {
            @Override
            public void onDoubleClick(DoubleClickEvent doubleClickEvent) {
                int selected=selectedTools.getSelectedIndex();
                toolsList.addItem(selectedTools.getItemText(selected));
                selectedTools.removeItem(selected);
            }
        });    */
    }

    public void updateWorkField() {
        flexTable.setVisible(true);
        for (int key : amounts.keySet()) {
            int am = Integer.parseInt(amounts.get(key).getValue());
            amountsValues.put(key, am);
        }
        flexTable.removeAllRows();
        int i = 0;
        for (int key : toolList.keySet()) {

            //  flexTable.setWidget( key.intValue(),0, new Label(workList.get(key).get(0)));
            flexTable.setWidget(i, 0, new Label(toolList.get(key)));

            TextBox amount = new TextBox();
            amount.setValue("" + amountsValues.get(key));
            amount.setStyleName(Recources.INSTANCE.styles().input());
            amounts.put(key, amount);


            flexTable.setWidget(i, 1, amounts.get(key));
            final Button removeButton = new Button("delete");
            removeButton.setStyleName(Recources.INSTANCE.styles().Caption());
            final int id = i;                                   // number of row
            final Integer CurKey = key;                         // work's id
            removeButton.addClickHandler(new ClickHandler() {
                public void onClick(ClickEvent event) {
                    toolsList.addItem(toolList.get(CurKey));
                    toolList.remove(CurKey);
                    amounts.remove(CurKey);
                    amountsValues.remove(CurKey);
                    removeRow(flexTable, id);
                }
            });
            buttonsForToolItem.put(key, removeButton);
            flexTable.setWidget(i, 2, buttonsForToolItem.get(key));


            i++;
        }

        //flexTable.getFlexCellFormatter().setRowSpan(0, 1, numRows + 1);
    }

    private void removeRow(FlexTable flexTable, int number) {
        int numRows = flexTable.getRowCount();
        if (numRows >= 0) {

            //flexTable.removeRow(numRows - 1);
            // flexTable.removeRow(number);


            flexTable.removeRow(number);
            updateWorkField();
        }
    }
}